package com.egg.basic.rabbitmq.sender;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.egg.basic.constant.BasicRabbitExchange;
import com.egg.common.core.constant.rabbit.RabbitRoutingKey;
import com.egg.common.util.IdGenUtil;
import com.egg.basic.model.rabbit.PayRabbitSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 发送
 *
 * @author east
 */
@Slf4j
@Component
public class PaySender {


    @Autowired(required = false)
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送-支付成功
     *
     * @param sender
     */
    public void paySuccess(PayRabbitSender sender) {
        if (ObjectUtil.isEmpty(sender)) {
            return;
        }
        String messageId = IdGenUtil.strId();
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setMessageId(messageId);
        Message message = new Message(JSON.toJSONString(sender).getBytes(), messageProperties);
        rabbitTemplate.convertAndSend(
                BasicRabbitExchange.PAY_EXCHANGE
                , RabbitRoutingKey.SUCCESS
                , message
        );
    }

}
